/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-08     balanceTWK   first version
 */

#ifndef __DRV_SOFT_I2C__
#define __DRV_SOFT_I2C__

#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>

/* soft i2c config class */
struct soft_i2c_config
{
    const rt_uint8_t scl;
    const rt_uint8_t sda;
    const char *bus_name;
    rt_uint32_t delay_us;  /* scl and sda line delay */
    rt_uint32_t timeout;   /* in tick */
};
/* i2c dirver class */
struct rt_soft_i2c_bus
{
    struct rt_i2c_bus_device i2c_bus;
};

#define SCL_MODE_IN(pin)    nu_gpio_mode(pin, PIN_MODE_INPUT)
#define SCL_MODE_OUT(pin)   nu_gpio_mode(pin, PIN_MODE_OUTPUT)
#define SDA_MODE_IN(pin)    nu_gpio_mode(pin, PIN_MODE_INPUT)
#define SDA_MODE_OUT(pin)   nu_gpio_mode(pin, PIN_MODE_OUTPUT)

//#define SCL_MODE_IN(pin)
//#define SCL_MODE_OUT(pin)
//#define SDA_MODE_IN(pin)
//#define SDA_MODE_OUT(pin)

#define SET_SDA(pin, val)   nu_gpio_write(pin, val)
#define SET_SCL(pin, val)   nu_gpio_write(pin, val)
#define GET_SDA(pin)        nu_gpio_read(pin)
#define GET_SCL(pin)        nu_gpio_read(pin)

#define SDA_L(cfg)          SET_SDA(cfg->sda, 0)
#define SDA_H(cfg)          SET_SDA(cfg->sda, 1)
#define SCL_L(cfg)          SET_SCL(cfg->scl, 0)

#ifdef BSP_USING_SOFT_I2C0
#define I2C0_BUS_CONFIG                                  \
    {                                                    \
        .scl = BSP_I2C0_SCL_PIN,                         \
        .sda = BSP_I2C0_SDA_PIN,                         \
        .bus_name = "i2c0",                              \
        .delay_us = 3,                                   \
        .timeout = 100,                                  \
    }
#endif

#ifdef BSP_USING_SOFT_I2C1
#define I2C1_BUS_CONFIG                                  \
    {                                                    \
        .scl = BSP_I2C1_SCL_PIN,                         \
        .sda = BSP_I2C1_SDA_PIN,                         \
        .bus_name = "i2c1",                              \
        .delay_us = 1,                                   \
        .timeout = 100,                                  \
    }
#endif

#ifdef BSP_USING_SOFT_I2C2
#define I2C2_BUS_CONFIG                                  \
    {                                                    \
        .scl = BSP_I2C2_SCL_PIN,                         \
        .sda = BSP_I2C2_SDA_PIN,                         \
        .bus_name = "i2c2",                              \
        .delay_us = 1,                                   \
        .timeout = 100,                                  \
    }
#endif

#ifdef BSP_USING_SOFT_I2C3
#define I2C3_BUS_CONFIG                                  \
    {                                                    \
        .scl = BSP_I2C3_SCL_PIN,                         \
        .sda = BSP_I2C3_SDA_PIN,                         \
        .bus_name = "i2c3",                              \
        .delay_us = 1,                                   \
        .timeout = 100,                                  \
    }
#endif

#ifdef BSP_USING_SOFT_I2C4
#define I2C4_BUS_CONFIG                                  \
    {                                                    \
        .scl = BSP_I2C4_SCL_PIN,                         \
        .sda = BSP_I2C4_SDA_PIN,                         \
        .bus_name = "i2c4",                              \
        .delay_us = 1,                                   \
        .timeout = 100,                                  \
    }
#endif
int rt_soft_i2c_init(void);

#endif  // __DRV_SOFT_I2C__
